#ifndef	_baseio_H_
#define	_baseio_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "predef.h"
//===================Port_Start===========================
#include "main.h"
#include "globlvar_debug.h"
//===================Port_Start===========================

#ifdef _freertosuse_
#include "main.h"
#include "cmsis_os.h"
#include "semphr.h"
#include "task.h"
#endif

#define	_pxSysTickTimer_IntEnable()		{	systickTimer.Instance->IER =1;	}
#define	_pxSysTickTimer_IntDisable()	{	systickTimer.Instance->IDR =1;	}
#define _pxSysTickTimer_IntState_Get()	 ((systickTimer.Instance->IVS &1)?1:0)

//#define SystemTick_Interrupt_On()		_pxSysTickTimer_IntEnable()//	{	SysTick->CTRL	|= SysTick_CTRL_TICKINT_Msk;	}	/* Turn on SysTick INT	*/
//#define	SystemTick_Interrupt_Off()	_pxSysTickTimer_IntDisable()// {	SysTick->CTRL	&= (~SysTick_CTRL_TICKINT_Msk); }	/* Turn off SysTick INT	*/

#define SystemTick_Interrupt_On()		{	SysTick->CTRL	|= SysTick_CTRL_TICKINT_Msk;	}	/* Turn on SysTick INT	*/
#define	SystemTick_Interrupt_Off()	{	SysTick->CTRL	&= (~SysTick_CTRL_TICKINT_Msk); }	/* Turn off SysTick INT	*/


#define	main_sequential_on()	SystemTick_Interrupt_On()
#define	main_sequential_off()	SystemTick_Interrupt_Off()

#define	_INTERRUPT_DISABLE()	__disable_irq()
#define	_INTERRUPT_ENABLE()	__enable_irq()

#define	disableInterrupts() __disable_irq()
#define	enableInterrupts() __enable_irq()

#define 	Pin_Control(port,pin,state)		HAL_GPIO_WritePin(port,pin,(state)?GPIO_PIN_SET:GPIO_PIN_RESET)
#define 	Pin_GetState(port,pin )	(( GPIO_PIN_SET ==	HAL_GPIO_ReadPin(port, pin))?1:0)
#define 	Pin_State_LeftDete(port,pin,data)		{ data *=2; if ( port->IDR & pin) data |=1;}

#ifdef	_freertosuse_
#define 	_Data_Int_exchange(des,sou)	{	taskENTER_CRITICAL(); ;memcpy((char *)&des,(char *)&sou,sizeof(des));	taskEXIT_CRITICAL();}
#define 	_Data_Int_exchange_ISR(des,sou)	 {	 uint32_t isrm; isrm=taskENTER_CRITICAL_FROM_ISR(); memcpy((char *)&des,(char *)&sou,sizeof(des));	taskEXIT_CRITICAL_FROM_ISR(isrm);}
#else
#define 	_Data_Int_exchange(des,sou)	{	u32 ltemp ;	ltemp = (SysTick->CTRL	& SysTick_CTRL_TICKINT_Msk);	main_sequential_off();lsystickstatu++;memcpy((char *)&des,(char *)&sou,sizeof(des));	if ( ltemp & SysTick_CTRL_TICKINT_Msk ) { lsystickstatu--;main_sequential_on();}}
#endif

#define _resetWdt()	{} //{	IWDG ->KR = 0xAAAA;}
#define	_NOP_ASM()	__NOP()

#ifdef _STM8_
#define	vWrite_STM8(Dst, Src, Length)	(((u8 (*)(u8*, uc8*, u8))(0x8084))(Dst, Src, Length))
#define	lSave(Dst,Src)	vWrite_STM8((u8 *)(&Dst), (u8 *)(&Src), sizeof(u32 ))
#define	iSave( Dst , Src )	vWrite_STM8((u8 *)(&Dst), (u8 *)(&Src), sizeof(u16 ))
#define	cSave( Dst , Src )	vWrite_STM8((u8 *)(&Dst), (u8 *)(&Src), sizeof(u8 ))
#define	lSetZero(Dst)		vWrite_STM8((u8 *)(&Dst), zeroP, sizeof(u32 ))
#define	cSetZero(Dst)	vWrite_STM8((u8 *)(&Dst), zeroP, sizeof(u8 ))

/*	Cortex M3 λ����	*/
#define	BITBAND_SRAM_REF	0x20000000
#define	BITBAND_SRAM_BASE	0x22000000
#define	BITBAND_SRAM(a, b)	(*(vu8 *)((BITBAND_SRAM_BASE + ((((u8*)(a))-((u8*)BITBAND_SRAM_REF))*32) + ((b)*4))))	// Convert SRAM address
#define	BITBAND_PERI_REF	0x40000000
#define	BITBAND_PERI_BASE	0x42000000
#define	BITBAND_PERI(a,b)	(*(vu8 *)((BITBAND_PERI_BASE + ((((u8*)(a))-((u8*)BITBAND_PERI_REF))*32) + ((b)*4))))	// Convert PERI address

enum ENUMPINCONFIG
{
	_INPUT_ = 0,
	_OUTPUT_,
};

enum ENUMJDSTATE
{
	_JDSTATE_OFF_ = 0,
	_JDSTATE_ON_,
};

enum ENUMLINE
{
	_LineNul_,
	_LineOn_,
	_LineOff_,
	_LineEnd_,
};

//		ȱʡ����
#define	_para_zero_to_def(value,def)	((value)?value:def)
#define	_para_zero_to_1(value)	_para_zero_to_def(value,1)

#endif
#endif
